<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="uid">
      <el-input v-model="dataForm.uid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="动态id" prop="nid">
      <el-input v-model="dataForm.nid" placeholder="动态id"></el-input>
    </el-form-item>
    <el-form-item label="评论id" prop="cid">
      <el-input v-model="dataForm.cid" placeholder="评论id"></el-input>
    </el-form-item>
    <el-form-item label="" prop="toUid">
      <el-input v-model="dataForm.toUid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="content">
      <el-input v-model="dataForm.content" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="likedCount">
      <el-input v-model="dataForm.likedCount" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="hateCount">
      <el-input v-model="dataForm.hateCount" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          uid: '',
          nid: '',
          cid: '',
          toUid: '',
          content: '',
          likedCount: '',
          hateCount: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          uid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          nid: [
            { required: true, message: '动态id不能为空', trigger: 'blur' }
          ],
          cid: [
            { required: true, message: '评论id不能为空', trigger: 'blur' }
          ],
          toUid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          likedCount: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          hateCount: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/common/newsreply/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.uid = data.newsReply.uid
                this.dataForm.nid = data.newsReply.nid
                this.dataForm.cid = data.newsReply.cid
                this.dataForm.toUid = data.newsReply.toUid
                this.dataForm.content = data.newsReply.content
                this.dataForm.likedCount = data.newsReply.likedCount
                this.dataForm.hateCount = data.newsReply.hateCount
                this.dataForm.createTime = data.newsReply.createTime
                this.dataForm.updateTime = data.newsReply.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/common/newsreply/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'uid': this.dataForm.uid,
                'nid': this.dataForm.nid,
                'cid': this.dataForm.cid,
                'toUid': this.dataForm.toUid,
                'content': this.dataForm.content,
                'likedCount': this.dataForm.likedCount,
                'hateCount': this.dataForm.hateCount,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
